package com.whfc.wxmp.api.quality.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.PageVO;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.AssertUtil;
import com.whfc.entity.param.check.CheckSearchParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.service.SysUserService;
import com.whfc.quality.dto.QualityTaskExecDTO;
import com.whfc.quality.param.QuailTaskCompleteParam;
import com.whfc.quality.param.QualityTaskReportParam;
import com.whfc.quality.service.QualityTaskService;
import com.whfc.wxmp.api.quality.service.WxQualityTaskService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description 检查任务
 * @Author hw
 * @Date 2021-05-18 10:18
 * @Version 1.0
 */
@Service
public class WxQualityTaskServiceImpl implements WxQualityTaskService {

    @DubboReference(interfaceClass = QualityTaskService.class, version = "1.0.0")
    private QualityTaskService qualityTaskService;

    @DubboReference(interfaceClass = SysUserService.class, version = "1.0.0")
    private SysUserService sysUserService;

    @Override
    public PageData<QualityTaskExecDTO> list(String phone, Integer deptId, Integer report, Date startTime, Date endTime, Integer pageSize, Integer pageNum) {
        if (report != null && 1 == report) {
            SysUser sysUser = sysUserService.getUserByPhone(phone);
            if (sysUser == null) {
                return new PageData<>();
            }
            return qualityTaskService.taskExecList(sysUser.getId(), deptId, null, startTime, endTime, pageNum, pageSize);
        } else {
            return qualityTaskService.taskExecList(deptId, null, startTime, endTime, pageNum, pageSize);
        }
    }

    @Override
    public ListData<QualityTaskExecDTO> list(String phone, Integer deptId, Integer report, Date startTime, Date endTime) {
        List<QualityTaskExecDTO> list;
        if (report != null && 1 == report) {
            SysUser sysUser = sysUserService.getUserByPhone(phone);
            if (sysUser == null) {
                return new ListData<>();
            }
            list = qualityTaskService.taskExecList(sysUser.getId(), deptId, null, startTime, endTime);
        } else {
            list = qualityTaskService.taskExecList(deptId, null, startTime, endTime);
        }
        return new ListData<>(list);
    }

    @Override
    public void execAdd(QualityTaskReportParam param, String phone) {
        SysUser sysUser = sysUserService.getUserByPhone(phone);
        if (sysUser == null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "请联系管理员添加账号");
        }
        param.setUserId(sysUser.getId());
        param.setUserName(sysUser.getNickname());
        qualityTaskService.report(param);
    }

    @Override
    public PageData<QualityTaskExecDTO> meList(String phone, Integer deptId, Integer pageSize, Integer pageNum) {
        SysUser sysUser = sysUserService.getUserByPhone(phone);
        if (sysUser == null) {
            return new PageData<>();
        }
        return qualityTaskService.taskExecList(sysUser.getId(), deptId, null, null, null, 1, null, null, pageNum, pageSize);
    }

    @Override
    public void execComplete(QuailTaskCompleteParam param) {
        qualityTaskService.complete(param);
    }

    @Override
    public PageVO<QualityTaskExecDTO> page(CheckSearchParam param, String phone) {
        param.setUserId(get(phone).getId());
        return qualityTaskService.getTaskPage(param);
    }

    private SysUser get(String phone) {
        AssertUtil.isTrue(StringUtils.isBlank(phone), "手机号信息缺失");
        SysUser user = sysUserService.getUserByPhone(phone);
        if (user == null) {
            throw new BizException(ResultEnum.NOT_FOUND.getCode(), "请联系管理员添加账号");
        }
        return user;
    }
}
